package com.alphathink.deliveryassist.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

/**
 * 网络操作的相关操作
 * 
 * @ClassName: NetOperater
 * @Description: TODO
 * @author AT xpg
 * @date 2015-4-2 下午9:34:33
 * 
 */
public class NetOperater {
	/**
	 * 版本检测
	 * 
	 * @Title: CheckVersion
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param url 版本检测的网络地址
	 * @param @return 设定文件
	 * @return String 返回类型 服务器中的版本信息
	 * @throws
	 * @author AT xpg
	 */
	public String CheckVersion(String url) {
		String serverVersion = "";
		try {
			HttpPost httpPost = new HttpPost(url);
			Log.d("版本检查时候的URL", url);
			HttpClient client = new DefaultHttpClient();
			HttpResponse httpResponse = client.execute(httpPost);
			List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
			// paramsList.add(new BasicNameValuePair("DOpostlogin", "DO"));

			httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
			if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				Log.d("版本检测网络状态码", httpResponse.getStatusLine().getStatusCode()
						+ "");
				httpPost.abort();
			} else {
				HttpEntity entity = httpResponse.getEntity();
				serverVersion = EntityUtils.toString(entity, "UTF_8").trim()
						.toString();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Log.d("检查版本网路操作", serverVersion);
		System.out.println("net----------------------" + serverVersion);
		return serverVersion;
	}

	/**
	 * 版本更新
	 * 
	 * @Title: updateVersion
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param updateVersion 版本更新的url
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 * @author AT xpg
	 */
	public String updateVersion(String updateVersion) {
		String update = "";
		try {
			HttpPost httpPost = new HttpPost(updateVersion);
			HttpClient client = new DefaultHttpClient();
			HttpResponse httpResponse = client.execute(httpPost);
			List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
			// paramsList.add(new BasicNameValuePair("DOpostlogin", "DO"));

			httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
			if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				Log.d("更新版本网络状态码", httpResponse.getStatusLine().getStatusCode()
						+ "");
				httpPost.abort();
			} else {
				HttpEntity entity = httpResponse.getEntity();
				update = EntityUtils.toString(entity, "UTF_8").trim()
						.toString();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return update;
	}

	/**
	 * 登陆功能修改密码
	 * 
	 * @param StrUname
	 * @param StrUpsw
	 * @return
	 */
	@SuppressWarnings("null")
	public String operaterNetUpdatePwd(String mUserName, String mPwd,
			String oldPwd, String host) {
		String json = "";
		try {

			String httpURL = host;
			System.out.println("login的时候host:" + httpURL);
			HttpPost httpPost = new HttpPost(httpURL);

			HttpClient client = new DefaultHttpClient();
			List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
			paramsList.add(new BasicNameValuePair("UserName", mUserName));
			paramsList.add(new BasicNameValuePair("UserPassword", oldPwd));
			paramsList.add(new BasicNameValuePair("NewPassword", mPwd));
			httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));

			HttpResponse response = client.execute(httpPost);
			System.out.println("====Login时服务器返回状态的状态码===="
					+ response.getStatusLine().getStatusCode());
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				httpPost.abort();
			} else {
				HttpEntity entity = response.getEntity();
				json = EntityUtils.toString(entity, "UTF_8").trim().toString();

			}
			client.getConnectionManager().shutdown();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return json;
	}

	/**
	 * 登陆功能
	 * 
	 * @param StrUname
	 * @param StrUpsw
	 * @return
	 */
	@SuppressWarnings("null")
	public String operaterNetLogin(String mUserName, String mPwd, String host) {
		String json = "";
		try {

			String httpURL = host;
			System.out.println("login的时候host:" + httpURL);
			HttpPost httpPost = new HttpPost(httpURL);

			HttpClient client = new DefaultHttpClient();
			List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
			paramsList.add(new BasicNameValuePair("UserName", mUserName));
			paramsList.add(new BasicNameValuePair("UserPassword", mPwd));
			httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));

			HttpResponse response = client.execute(httpPost);
			System.out.println("====Login时服务器返回状态的状态码===="
					+ response.getStatusLine().getStatusCode());
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				httpPost.abort();
			} else {
				HttpEntity entity = response.getEntity();
				json = EntityUtils.toString(entity, "UTF_8").trim().toString();

			}
			client.getConnectionManager().shutdown();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return json;
	}

}
